package com.ytjj.qmyx.mall.controller;

import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.mall.model.AdvertCallback;
import com.ytjj.qmyx.mall.model.EffectParamRequest;
import com.ytjj.qmyx.mall.service.AdvertCallBackService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RequestMapping("/advertCb")
@RestController
@Slf4j
public class AdvertCallBackController {
    private static final String TOUTIAO_SECRET="eYxzkHS-pJxNj-vta-OgOEzCHyHGesuOt";
    private static final String ADVERT_TT="TT";
    private static final String ADVERT_KS="KS";
    private static final String PLATFORM_ANDROID="android";
    private static final String PLATFORM_IOS="ios";

    @Resource
    private AdvertCallBackService advertCallBackService;

    /**
     * 快手监测
     * 快手  __OS__   0:Android，1:iOS，3:其他
     * imeiMD5为md5后的值
     * idfa为md5后的值
     * oaid为原值
     */
    @GetMapping("kuaishou")
    public CommonResult kuaishou(@RequestParam(value="channel",required = false) String channel
                                    ,@RequestParam(value="platform",required = false) String platform
                                    ,@RequestParam(value="imeiMD5",required = false) String imeiMD5
                                    ,@RequestParam(value="oaid",required = false) String oaid
                                    ,@RequestParam(value="idfa",required = false) String idfa
                                    ,@RequestParam(value="ip",required = false) String ip
                                    ,@RequestParam(value="ua",required = false) String ua
                                    ,@RequestParam(value="callback",required = false) String callback
                                    ,@RequestParam(value="campaignid",required = false) String campaignid
                                    ,@RequestParam(value="os",required = false) String os
                                    ,@RequestParam(value="type",required = false) String type
                                ) throws Exception {
        log.info("kuaishou:channel={},platform={},imeiMD5={},oaid={},idfa={},ip={},ua={},campaignid={},callback_url={},os={}"
                ,channel,platform,imeiMD5,oaid,idfa,ip,ua,campaignid,callback,os);
        platform="other";
        if("0".equals(os)){
            platform=PLATFORM_ANDROID;
        }else if("1".equals(os)){
            platform=PLATFORM_IOS;
        }
        AdvertCallback advertCallback = new AdvertCallback();
        advertCallback.setChannel(channel);
        advertCallback.setPlatform(platform);
        advertCallback.setType(type);
        if(PLATFORM_ANDROID.equals(platform)) {
            advertCallback.setImei(imeiMD5);
            advertCallback.setOaid(oaid);
        }else if(PLATFORM_IOS.equals(platform)) {
            advertCallback.setIdfa(idfa);
        }
        advertCallback.setIp(ip);
        advertCallback.setUa(ua);
        advertCallback.setCallback(callback);
        advertCallback.setCid(campaignid);
        advertCallback.setAdvertPlatform(ADVERT_KS);
        advertCallBackService.saveAdvertCallBack(advertCallback);
        return CommonResult.success();
    }
    /**
     * 头条监测
     * 头条 OS   安卓：0   IOS：1  其他：3
     * imei为md5后的值
     * idfa为原值
     * oaid为原值
     */
    @GetMapping("toutiao")
    public CommonResult toutiao(@RequestParam(value="channel",required = false) String channel
                                    ,@RequestParam(value="aid",required = false) String aid
                                    ,@RequestParam(value="imei",required = false) String imei
                                    ,@RequestParam(value="oaid",required = false) String oaid
                                    ,@RequestParam(value="idfa",required = false) String idfa
                                    ,@RequestParam(value="ip",required = false) String ip
                                    ,@RequestParam(value="ua",required = false) String ua
                                    ,@RequestParam(value="cid",required = false) String cid
                                    ,@RequestParam(value="callback_url",required = false) String callback_url
                                    ,@RequestParam(value="os",required = false) String os
                                    ,@RequestParam(value="type",required = false) String type
                                ) throws Exception {
        log.info("toutiao:channel={},aid={},imei={},oaid={},idfa={},ip={},ua={},cid={},callback_url={},os={}"
                    ,channel,aid,imei,oaid,idfa,ip,ua,cid,callback_url,os);
        if (StringUtils.isEmpty(type)) {
            return CommonResult.success();
        }
        if("__CALLBACK_URL__".equals(callback_url)){
            return CommonResult.success();
        }
        String platform="other";
        if("0".equals(os)){
            platform=PLATFORM_ANDROID;
        }else if("1".equals(os)){
            platform=PLATFORM_IOS;
        }
        AdvertCallback advertCallback = new AdvertCallback();
        advertCallback.setChannel(channel);
        advertCallback.setPlatform(platform);
        advertCallback.setType(type);
        if(PLATFORM_ANDROID.equals(platform)) {
            advertCallback.setImei(imei);
            advertCallback.setOaid(oaid);
        }else if(PLATFORM_IOS.equals(platform)) {
            advertCallback.setIdfa(idfa);
        }
        advertCallback.setAid(aid);
        advertCallback.setIp(ip);
        advertCallback.setUa(ua);
        advertCallback.setCallback(callback_url);
        advertCallback.setCid(cid);
        advertCallback.setAdvertPlatform(ADVERT_TT);
        advertCallBackService.saveAdvertCallBack(advertCallback);
        return CommonResult.success();
    }

    @PostMapping("/sendEffectData")
    public CommonResult sendEffectData(@RequestBody EffectParamRequest request) {
        advertCallBackService.sendEffectData(request);
        return CommonResult.success();
    }

}
